package com.aidex.biz.controller;

import com.aidex.biz.domain.CustomerReceive;
import com.aidex.biz.service.CustomerReceiveService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 我的接单Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-04-23
 */
@RestController
@RequestMapping("/biz/bizCustomerReceive")
public class CustomerReceiveController extends BaseController {
    @Autowired
    private CustomerReceiveService customerReceiveService;

    /**
     * 查询我的接单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerReceive:list')")
    @GetMapping("/list")
    public R<PageInfo> list(CustomerReceive customerReceive, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerReceive.setCompanyId(null);
        } else {
            customerReceive.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        customerReceive.setPage(new PageDomain(request, response));
        return R.data(customerReceiveService.findPage(customerReceive));
    }


    /**
     * 获取我的接单详细信息
     */
    //@PreAuthorize("@ss.hasPermi('biz:bizCustomerReceive:query')")
    @GetMapping(value = "/{id}")
    public R<CustomerReceive> detail(@PathVariable("id") String id) {
        return R.data(customerReceiveService.get(id));
    }

    /**
     * 新增我的接单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerReceive:add')")
    @Log(title = "我的接单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated CustomerReceive customerReceive) {
        return R.status(customerReceiveService.save(customerReceive));
    }

    /**
     * 修改我的接单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerReceive:edit')")
    @Log(title = "我的接单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated CustomerReceive customerReceive) {
        return R.status(customerReceiveService.save(customerReceive));
    }


    /**
     * 删除我的接单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerReceive:remove')")
    @Log(title = "我的接单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(customerReceiveService.deleteCustomerReceiveByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerReceive:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(customerReceiveService.findMaxSort(new CustomerReceive()));
    }

    /**
     * 导出我的接单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerReceive:export')")
    @Log(title = "我的接单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(CustomerReceive customerReceive) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerReceive.setCompanyId(null);
        } else {
            customerReceive.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<CustomerReceive> list = customerReceiveService.findList(customerReceive);
        ExcelUtil<CustomerReceive> util = new ExcelUtil<CustomerReceive>(CustomerReceive.class);
        return util.exportExcel(list, "我的接单数据");
    }

}
